/**
 * 二次封装uniapp 一些API
 */

import {
	requestPermission
} from "@/tools/permision.js"

const platform = uni.getSystemInfoSync().platform;
const isIos = platform == 'ios'
const isAndroid = platform == 'android'

/**
 * 1、选择相册照片
 * 2、相机拍照
 * @param {Object} params 参数
 * @param {Array} params.sourceType album-从相册选图；camera-使用相机，默认二者都有。如需直接开相机或直接选相册，请只使用一个选项。
 * @param {Number} params.count 最多可以选择的图片张数，默认9
 * @param {Object} params.crop 裁剪参数，uniapp自带裁剪
 * return uni.chooseImage的原返回值
 */
export const uniApiBaseChooseImage = (params) => {
	return new Promise(async (resolve, reject) => {
		const {
			sourceType,
			count,
			crop
		} = params;

		if (sourceType.length == 1 && sourceType[0] == "album") {
			const _requestPermission = await requestPermission("photoLibrary");
			if (_requestPermission != 1) {
				// 只ios才显示权限引导
				if (isIos) {
					reject({
						errMsg: "show-permision-guide"
					})
				}
				return
			}
		}
		if (sourceType.length == 1 && sourceType[0] == "camera") {
			const _cameraPermission = await requestPermission("camera");
			if (_cameraPermission != 1) {
				// 只ios才显示权限引导
				if (isIos) {
					reject({
						errMsg: "show-permision-guide"
					})
				}
				return
			}
		}
		uni.chooseImage({
			count: count || 1,
			sourceType: sourceType || ["album", "camera"],
			crop: crop || null,
			sizeType: ["compressed"],
			success: (res) => {
				resolve(res)
			},
			fail: error => {
				console.log('uniApiBaseChooseImage fail***', error)
				reject(error)
			}
		})
	})
}

/**
 * 1、选择相册视频
 * 2、拍摄视频
 * @param {Object} params 参数
 * @param {Array} params.sourceType 图片和视频选择的来源
 * @param {Number} params.count 最多可以选择的文件个数
 * @param {Number} params.maxDuration 默认值10s，拍摄视频最长拍摄时间，单位秒。时间范围为 3s 至 30s 之间
 */
export const uniApiBaseChooseVideo = (params) => {
	return new Promise(async (resolve, reject) => {
		const {
			sourceType,
			count,
			maxDuration
		} = params;

		if (sourceType.length == 1 && sourceType[0] == "album") {
			const _requestPermission = await requestPermission("photoLibrary");
			if (_requestPermission != 1) {
				// 只ios才显示权限引导
				if (isIos) {
					reject({
						errMsg: "show-permision-guide"
					})
				}
				return
			}
		}
		if (sourceType.length == 1 && sourceType[0] == "camera") {
			const _cameraPermission = await requestPermission("camera");
			if (_cameraPermission != 1) {
				// 只ios才显示权限引导
				if (isIos) {
					reject({
						errMsg: "show-permision-guide"
					})
				}
				return
			}

			const perMicroResult = await requestPermission("microphone");
			if (perMicroResult != 1) return
		}

		uni.chooseVideo({
			count: count || 1,
			sourceType: sourceType || ['album', 'camera'],
			maxDuration: maxDuration || 10,
			success: res => {
				resolve(res)
			},
			fail: error => {
				console.log('uniApiBaseChooseVideo fail***', error)
				reject(error)
				// errCode
				// 0-chooseVideo:fail cancel
				// 11-chooseVideo:fail resultCode is wrong
				// 12-chooseVideo:fail User cancelled
				// const failCode = [0, 11, 12]
				// if (!failCode.includes(res.errCode)) {
				// 	uni.showToast({
				// 		title: res.errMsg,
				// 		icon: 'none'
				// 	})
				// }
			}
		});
	})
}